/**
 * @Title: 心跳类
 * @Package tt.dz.mvc.controller
 * @author pengyu
 * @date 2015年12月17日 上午10:21:55
 * @version V1.0
 */
package tt.dz.mvc.controller.middleware;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.exception.BizException;
import tt.dz.mvc.controller.base.BaseController;
import tt.dz.service.imp.middleware.Ev3cMessageServiceImpl;
import tt.dz.service.middleware.MessageService;
import tt.dz.service.middleware.SetService;
import tt.dz.service.sys.ChargerService;
import tt.dz.tcp.socket.exception.CreateConnectionFailException;
import tt.dz.tcp.socket.exception.DuplicateInstructionException;
import tt.dz.tcp.socket.exception.RetryFailException;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

@Controller
public class SetController extends BaseController
{
	/**
	 * 日志对象
	 */
	private Log log = LogFactory.getLog(SetController.class);
	@Autowired
	SetService setService;
	@Autowired
	ChargerService chargerService;
	@Resource(name = "ev3cMessageServiceImpl")
	MessageService ev3cMessageService;

	//查询硬件服务器ip
	@RequestMapping("/findHardwareServerIp")
	@ResponseBody
	public JsonCommon<String> findHardwareServerIp(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		String protocol_type = map.get("protocol_type");
		String protocol_version = map.get("protocol_version");
		String data = map.get("data");
		if (StringUtil.isBlank(protocol_type) || StringUtil.isBlank(protocol_version))
		{
			JsonCommon<String> json = new JsonCommon<String>();
			json.setCode("400");
			json.setMsg("protocol_type or protocol_version or data null");
		}
		return setService.findHardwareServerIp(map);
	}

	//查询硬件服务器ip返回 
	// 查询硬件服务器ip地址接口    服务器下发:充电桩回复   case "31":
	@RequestMapping("/findHardwareServerIpReturn")
	@ResponseBody
	public JsonCommon<Map<String, String>> findHardwareServerIpReturn(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByMiddle(request);
		return setService.findHardwareServerIpReturn(map);
	}

	//设置硬件服务器ip    服务器下发
	@RequestMapping("/setHardwareServerIp")
	@ResponseBody
	public JsonCommon<String> setHardwareServerIp(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		//		String protocol_type = map.get("protocol_type");
		//		String protocol_version = map.get("protocol_version");
		//		String data = map.get("data");
		//		if(StringUtil.isBlank(protocol_type)||StringUtil.isBlank(protocol_version)||StringUtil.isBlank(data)){
		//			JsonCommon<String> json = new JsonCommon<String>();
		//			json.setCode("400");
		//			json.setMsg("protocol_type or protocol_version or data null");
		//		}
		return setService.setHardwareServerIp(map);
	}

	//设置硬件服务器ip 返回
	@RequestMapping("/setHardwareServerIpReturn")
	@ResponseBody
	public JsonCommon<Map<String, String>> setHardwareServerIpReturn(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByMiddle(request);
		return setService.setHardwareServerIpReturn(map);
	}

	//6.3.4电桩费率查询     服务器下发
	@RequestMapping("/findRate")
	@ResponseBody
	public JsonCommon<String> findRate(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		return setService.findRate(map);
	}

	//设置对时
	@RequestMapping("/setTimer")
	@ResponseBody
	public JsonCommon<String> setTimer(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		//		Map<String,String> map = StringUtil.getParameterByOms(request);
		String chargerId = request.getParameter("chargerId");
		String timer = request.getParameter("time");
		return setService.setTimer(chargerId, timer);
	}

	//6.3.5电桩费率设置     服务器下发
	@RequestMapping("/setRate")
	@ResponseBody
	public JsonCommon setRate(String chargerId) throws CreateConnectionFailException, BizException, RetryFailException
	{
		setService.sendFee(chargerId);
		return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	//6.3.6电桩运营二维码设置     服务器下发
	@RequestMapping("/setQrCode")
	@ResponseBody
	public JsonCommon<String> setQrCode(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		String newQrCode = chargerService.updateQrCode(map.get("chargerId"));
		map.put("qrCode", newQrCode);
		return setService.setQrCode(map);
	}

	//6.3.7电桩白名单卡号查询     服务器下发
	@RequestMapping("/findWhiteList")
	@ResponseBody
	public JsonCommon<String> findWhiteList(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		return setService.findWhiteList(map);
	}

	//	//6.3.8电桩白名单卡号设置    服务器下发
	//	@RequestMapping("/setWhiteList")
	//	@ResponseBody
	//	public JsonCommon<String> setWhiteList(HttpServletRequest request, HttpServletResponse response) throws Exception
	//	{
	//		Map<String, String> map = StringUtil.getParameterByOms(request);
	//		return setService.setWhiteList(map);
	//	}

	//6.3.9电桩参数查询   服务器下发
	@RequestMapping("/findParam")
	@ResponseBody
	public JsonCommon<String> findParam(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		map.put("control_type", "00");
		return setService.setParam(map);
	}

	//6.3.9电桩参数设置    服务器下发
	@RequestMapping("/setParam")
	@ResponseBody
	public JsonCommon<String> setParam(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		map.put("control_type", "01");
		return setService.setParam(map);
	}

	//1.1 召唤指令  
	@RequestMapping("/summon")
	@ResponseBody
	public JsonCommon<String> summon(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		Map<String, String> map = StringUtil.getParameterByOms(request);
		return setService.summon(map);
	}

	//设置状态
	@RequestMapping("/setState")
	@ResponseBody
	public JsonCommon setState(String chargerId, String state) throws Exception
	{
		setService.setState(chargerId, state);
		return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	//设置标识
	@RequestMapping("/setIdentification")
	@ResponseBody
	public JsonCommon setIdentification(String chargerId) throws CreateConnectionFailException, BizException, RetryFailException
	{
		setService.setIdentification(chargerId);
		return new JsonCommon(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}

	//查询标识
	@RequestMapping("/findIdentification")
	@ResponseBody
	public JsonCommon<String> findIdentification(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String chargerId = request.getParameter("chargerId");
		return setService.findIdentification(chargerId);
	}

	//查询桩状态
	@RequestMapping("/findChargerStatus")
	@ResponseBody
	public JsonCommon<String> findChargerStatus(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String chargerId = request.getParameter("chargerId");
		String portNo = request.getParameter("portNo");
		return setService.findChargerStatus(chargerId, portNo);
	}

	//查询桩卡号
	@RequestMapping("/findUserCardOnCharger")
	@ResponseBody
	public JsonCommon<String> findUserCardOnCharger(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		String chargerId = request.getParameter("chargerId");
		return setService.findUserCardOnCharger(chargerId);
	}

	@RequestMapping("/setip")
	@ResponseBody
	public JsonCommon<String> setip(String chargerId, String ip, String port) throws CreateConnectionFailException, RetryFailException, DuplicateInstructionException
	{
		String main_hardware_ip = StringUtil.ip2HexStrLeftComplement(ip, 2);
		String main_port = StringUtil.float10to16ByParamDesc(port, 1, 4);

		String ev3c_msgBody = "00" + StringUtil.now14time() + main_hardware_ip + main_port + main_hardware_ip + main_port + "00";
		String ev3c_msgHeader = "68" + StringUtil.ev3cMsgBodyBin(ev3c_msgBody) + "01" + "000000" + "0001" + chargerId + "32" + StringUtil.now14time();

		try
		{
			ev3cMessageService.sendNoWait(ev3c_msgHeader + ev3c_msgBody, chargerId);
		}
		catch (RetryFailException | CreateConnectionFailException | DuplicateInstructionException e)
		{
			log.error(e.getMessage(), e);
			throw e;
		}
		return new JsonCommon(null, OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
}
